package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQueryObject;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private StatementMapper statementMapper;




    @Override
    public TablePageInfo<StatementItem> query(StatementItemQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

//    @Override
//    public void saveItems(List<StatementItem> items) {
//        //最后一项----打折金额
//        StatementItem tempItem = items.remove(items.size() - 1);
//        //打折金额
//        BigDecimal discountAmount = tempItem.getItemPrice();
//        //计算单id
//        Long statementId = tempItem.getStatementId();
//        //
//        statementItemMapper.deleteByPrimaryKey(statementId);
//
//        //根据结算单ID删除statementItem的记录
//        statementItemMapper.deleteByStatementId(statementId);
//        if (items.size() > 0) {
//            for (StatementItem item : items) {
//                statementItemMapper.insert(item);
//                totalQuantity = totalQuantity.add(item.getItemQuantity());
//                totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
//            }
//        }
//
//        if (totalAmount.comparaTo(discountAmount) < 0) {
//            throw new BusinessException("非法操作");
//        }
//        statementMapper.updataAmount(statementId, totalAmount, totalQuantity, discountAmount);
//    }

    @Override
    public void saveItems(List<StatementItem> items) {
        StatementItem tempItem = items.remove(items.size() - 1);
        //打折金额
        BigDecimal discountAmount = tempItem.getItemPrice();
        // 计算单 ID
        Long statementId = tempItem.getStatementId();
        //根据结算单ID删除statementItem的记录    先删后加
        statementItemMapper.deleteByStatementId(statementId);
        //记总金额
        BigDecimal totalAmount = new BigDecimal(0);
        BigDecimal totalQuantity = new BigDecimal(0);
        //判断是否有数据
        if (items.size()>0){
            for (StatementItem item : items) {
                statementItemMapper.insert(item);
                //统计 总数量
                totalQuantity = totalQuantity.add(item.getItemQuantity());
                //统计总金额 项目价格 * 项目数量
                totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
            }
        }
        //判断总金额不能低于打折金额
        if (totalAmount.compareTo(discountAmount)<0){
            throw new BusinessException("非法操作，优惠金额不能低于总金额");
        }

        //更新数据
        statementMapper.updateAmount(statementId,totalAmount,totalQuantity,discountAmount);

    }
}
